/*
	Copyright (C) 2010 2011 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef TASKTREECOLUMNSELECTDIALOG_H
#define TASKTREECOLUMNSELECTDIALOG_H

#include <QList>
#include <QDialog>
#include <QAbstractItemModel>
#include <QListView>
#include <QMouseEvent>

#include <procycle/task.h>

#include "pcchild.h"
#include "ui_tasktreecolumnselectdialog.h"


class TaskTreeModel;


class ColViewModel : public QAbstractItemModel, public PCChild {
public:
	ColViewModel (ProCycle* app, QList<PC::Task::Data> cols)
		: PCChild(app), m_cols(cols) {}

	~ColViewModel() {
	}

	int columnCount ( const QModelIndex & /*parent*/ = QModelIndex() ) const {
		return 1;
	}
	QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const;

	QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const {
		if (!hasIndex(row, column, parent)) {
			return QModelIndex();
		}
		PC::Task::Data td = m_cols.value(row, PC::Task::DATA_COUNT);
		if (td != PC::Task::DATA_COUNT) {
			return createIndex(row, column, int(td));
		}
		return QModelIndex();
	}
	QModelIndex parent ( const QModelIndex & /*index*/ ) const {
		return QModelIndex();
	}
	int rowCount ( const QModelIndex & /*parent*/ = QModelIndex() ) const {
		return m_cols.count();
	}

	bool setData(const QModelIndex &index, const QVariant &value, int role) {
		if (!index.isValid()) {
			return false;
		}
		if (role == Qt::DisplayRole) {
			bool ok;
			PC::Task::Data td = PC::Task::Data(value.toInt(&ok));
			if (!ok) {
				return false;
			}
			if (int(td) < 0 || int(td) >= PC::Task::DATA_COUNT) {
				return false;
			}
			m_cols.replace(index.row(), td);
			emit dataChanged(index, index);
			return true;
		}
		return false;
	}

	bool insertRow(int row, QVariant value, const QModelIndex& parent = QModelIndex()) {
		if (row < 0 || row > m_cols.size()) {
			return false;
		}
		beginInsertRows(parent, row, row);bool ok;
		PC::Task::Data td = PC::Task::Data(value.toInt(&ok));
		if (!ok) {
			return false;
		}
		if (int(td) < 0 || int(td) >= PC::Task::DATA_COUNT) {
			return false;
		}
		m_cols.insert(row, td);
		endInsertRows();
		return true;
	}

	bool removeRow(int row, const QModelIndex &parent = QModelIndex()) {
		if (row < 0 || row >= m_cols.size()) {
			return false;
		}
		beginRemoveRows(parent, row, row);
		m_cols.removeAt(row);
		endRemoveRows();
		return true;
	}


	PC::Task::Data col (int row) {
		return m_cols.value(row);
	}

private:
	QList<PC::Task::Data>	m_cols;
};


class ColListView : public QListView {
public:
	ColListView (QWidget* parent=0);
	virtual ~ColListView() {}

protected:
	void dragEnterEvent(QDragEnterEvent *event);
	void dragLeaveEvent(QDragLeaveEvent* event);
	void dragMoveEvent(QDragMoveEvent *event);
	void dropEvent(QDropEvent *event);
	void mouseMoveEvent(QMouseEvent *e);
	void mousePressEvent(QMouseEvent *event) {
		if (event->button() == Qt::LeftButton) {
			m_dragStartPosition = event->pos();
		}
	}

	void paintEvent(QPaintEvent *e);

private:
	QPoint m_dragStartPosition;
	int m_currInsertRow;
};


class TaskTreeColumnSelectDialog : public QDialog, public PCChild
{
	Q_OBJECT

public:
	explicit TaskTreeColumnSelectDialog(ProCycle *parent = 0);
	~TaskTreeColumnSelectDialog();

	QList<PC::Task::Data> columns();

private:
	Ui::TaskTreeColumnSelectDialog ui;

	TaskTreeModel*		m_model;

	ColListView*		m_avails;
	ColListView*		m_currs;
};

#endif // TASKTREECOLUMNSELECTDIALOG_H
